package com.changan.controller;

import com.aliyuncs.exceptions.ClientException;
import com.changan.response.Result;
import com.changan.service.OssService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@CrossOrigin
@Tag(name = "阿里云的OSS管理", description = "阿里云的OSS管理")
@Validated
@RestController
@RequestMapping("/oss")
public class OssController {

    @Resource
    private OssService ossService;

    @Operation(summary = "创建Bucket", description = "创建Bucket")
    @GetMapping("/createBucket")
    public Result createBucket(@RequestParam(value = "bucketName", required = false) String bucketName) throws ClientException {
        ossService.createBucket(bucketName);
        return Result.ok();
    }

    @Operation(summary = "删除Bucket", description = "删除Bucket")
    @GetMapping("/deleteBucket")
    public Result deleteBucket(String bucketName) {
        ossService.deleteBucket(bucketName);
        return Result.ok();
    }

    @Operation(summary = "上传文件", description = "上传文件到阿里云OSS")
    @PostMapping("/uploadFile")
    public Result uploadFile(@RequestParam(value = "file") MultipartFile file) {
        String url = ossService.uploadFile(file);
        return Result.ok().data("url", url);
    }

    @Operation(summary = "下载文件", description = "下载文件")
    @GetMapping("/downloadFile")
    public Result downloadFile(String url) {
        ossService.downloadFile(url);
        return Result.ok();
    }

    @Operation(summary = "删除文件", description = "删除文件")
    @GetMapping("/deleteFile")
    public Result deleteFile(@RequestParam(value = "url") String url) {
        ossService.deleteFile(url);
        return Result.ok();
    }

    @Operation(summary = "列举文件", description = "列举文件")
    @GetMapping("/listFile")
    public Result listFile() {
        return ossService.listFile();
    }

}
